﻿package com.designoutput.elements
{
	import com.designoutput.enum.ElementType;
	import com.designoutput.utils.Globals;
	import com.designoutput.utils.HitTestUtils;
	import com.designoutput.utils.Unit;
	import com.designoutput.utils.UnitUtils;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	/**
	 * 
	 * <br>ImageElement -> BaseRectElement -> BaseElement ->Base
	 */
	public class ImageElement extends BaseRectElement
	{
		private var _imageBytes:ByteArray;
		private var _image:DisplayObject;
		private var _urlLoader:URLLoader;
		private var _loader:Loader;
		private var _imageUrl:String;
		private var _originalProportion:Number; // original image proporation


		public function ImageElement()
		{
			super();
			_originalProportion = 0;
		}
		
		override public function getClassName():String{
			return ElementType.ImageElement; 
		}

		public function getImage():DisplayObject
		{
			return _image;
		}

		public function getOriginalProportion():Number
		{
			return _originalProportion;
		}

		public function loadImage(url:String):void
		{
			_imageUrl=url;
			if(_image!=null && this.contains(this._image)){
				this.removeChild(_image);
			}
			var request:URLRequest=new URLRequest(Globals.applicationUrl + _imageUrl);
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			_urlLoader.addEventListener(Event.COMPLETE, urlLoader_completeHandler);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			_urlLoader.load(request);
		}

		private function urlLoader_completeHandler(evt:Event):void
		{
			_imageBytes = _urlLoader.data;
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_completeHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			_loader.loadBytes(_imageBytes);
			_urlLoader = null;
		}
		
		private function loader_completeHandler(evt:Event):void
		{
			_image = _loader.content;
			if(_image is Bitmap)
				(_image as Bitmap).smoothing = true;
			
			addChild(_image);
			
			_originalProportion = _image.height / _image.width;
			
			updateElement();
			
			_loader = null;
		}

		private function errorHandler(evt:IOErrorEvent):void
		{
			_imageBytes = null;
			_urlLoader = null;
			_loader = null;
			trace(evt.toString());
		}


		override protected function updateElement():void
		{
			super.updateElement();
			if(_image != null){
				_image.width = _width;
				_image.height = _height;
			}
		}
		
		override public function toXML():XML
		{
			var elementXML:XML = super.toXML();
			elementXML.@text = _imageUrl;
			return elementXML;
		}

		override public function fromXML(xmlNode:XML):void
		{
			super.fromXML(xmlNode);
			loadImage(xmlNode.@text);
		}
		
		override public function hitTestPoint(x:Number, y:Number, shapeFlag:Boolean=false):Boolean {
			return super.hitTestPoint(x, y, shapeFlag);
		}
	}
}